rm(list=ls())


setwd("")

### 36 month model, main paper
load("model_output_synth.RData")

plot_data <- data.frame(multi$df.plot) # actual plot data

pdf("Analysis/Figures/Figure_synth.pdf", width=20, height=10)
par(mar=c(5,5,4,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value, na.rm=T)), 
     type="l", bty="l", ylab="Mean Personalism", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Number of Months Before and After a Failed Coup", 
     main="")
lines(lowess(plot_data[plot_data$var=="Treated",]$value, f=.12), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value, f=.12), lwd=3, col="black", lty=2)
abline(v=round(nrow(plot_data)/4+1), col="maroon", lwd=3)
axis(1, at=seq(1, 73, 72/6), labels = c(36, 24, 12, 0, 12, 24, 36), cex.axis=2)
legend(0, 0.53,legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
dev.off()



##### ALL COUNTRIES:

pdf("Analysis/Figures/all_countries_synth.pdf", width=20, height=15)
par(mfrow=c(3,2))
### SIERRA LEONE

load("Data/model_output_SL.RData")

plot_data <- data.frame(SL$df.plot) # actual plot data

#pdf("Figure_synth_SL.pdf", width=20, height=10)
par(mar=c(5,5,4,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value, na.rm=T)), 
     type="l", bty="l", ylab="Mean Personalism", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Number of Months Before and After a Failed Coup", 
     main="Sierra Leone - Siaka Stevens (1968-1973)", cex.main=3)
lines(lowess(plot_data[plot_data$var=="Treated",]$value, f=0.3), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value, f=0.2), lwd=3, col="black", lty=2)
abline(v=round(nrow(plot_data)/4), col="maroon", lwd=4)
axis(1, at=c(2, 14, 26, 38, 50), labels = c(24, 12, 0, 12, 24), cex.axis=2)
legend(0, 0.59,legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
#dev.off()


### ALGERIA
load("Data/model_output_ALG.RData")

plot_data <- data.frame(ALG$df.plot) # actual plot data

#pdf("Figure_synth_ALG.pdf", width=20, height=10)
par(mar=c(5,5,4,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value, na.rm=T)), 
     type="l", bty="l", ylab="", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Months", 
     main="Algeria - Houari Boumédiène (1965-1970)",cex.main=3)
lines(lowess(plot_data[plot_data$var=="Treated",]$value, f=0.17), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value, f=0.17), lwd=3, col="black", lty=2)
abline(v=round(nrow(plot_data)/4)-2, col="maroon", lwd=1)
axis(1, at=c(3, 15, 27, 39, 51), labels = c(24, 12, 0, 12, 24), cex.axis=2)
#legend(0, max(plot_data$value),legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
#dev.off()


### BANGLADESH
load("Data/model_output_BAN.RData")

plot_data <- data.frame(BAN$df.plot) # actual plot data

#pdf("Figure_synth_BAN.pdf", width=20, height=10)
par(mar=c(5,5,6,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value+0.02, na.rm=T)), 
     type="l", bty="l", ylab="", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Months", 
     main="Bangladesh - Zia Rahman (1976-1979)", cex.main=3)
lines(lowess(plot_data[plot_data$var=="Treated",]$value, f=.2), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value, f=.2), lwd=3, col="black", lty=2)
abline(v=round(nrow(plot_data)/4), col="maroon", lwd=4)
axis(1, at=c(0, 9, 21, 33, 41), labels = c(21, 12, 0, 12, 21), cex.axis=2)
#legend(0, max(plot_data$value),legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
#dev.off()


### BENIN
load("Data/model_output_BEN.RData")

plot_data <- data.frame(BEN$df.plot) # actual plot data

#pdf("Figure_synth_BEN.pdf", width=20, height=10)
par(mar=c(5,5,6,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value, na.rm=T)), 
     type="l", bty="l", ylab="", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Months", 
     main="Benin - Mathieu Kérékou (1973-1977)", cex.main=3)
lines(lowess(plot_data[plot_data$var=="Treated",]$value, f=0.2), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value, f=0.2), lwd=3, col="black", lty=2)
abline(v=round(nrow(plot_data)/4), col="maroon", lwd=4)
axis(1, at=c(2, 14, 26, 38, 50), labels = c(24, 12, 0, 12, 24), cex.axis=2)
#legend(0, max(plot_data$value),legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
#dev.off()


### Iraq
load("Data/model_output_IRAQ.RData")

plot_data <- data.frame(IRAQ$df.plot) # actual plot data

#pdf("Figure_synth_IRAQ.pdf", width=20, height=10)
par(mar=c(5,5,6,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value, na.rm=T)), 
     type="l", bty="l", ylab="", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Months", 
     main="Iraq - H. al-Bakr/S. Hussein (1971-1975)", cex.main=3)
lines(lowess(plot_data[plot_data$var=="Treated",]$value, f=0.2), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value, f=0.2), lwd=3, col="black", lty=2)
abline(v=round(nrow(plot_data)/4), col="maroon", lwd=4)
axis(1, at=c(2, 14, 26, 38, 50), labels = c(24, 12, 0, 12, 24), cex.axis=2)
#legend(0, max(plot_data$value),legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
#dev.off()


### AZE
load("Data/model_output_AZE.RData")

plot_data <- data.frame(AZE$df.plot) # actual plot data

#pdf("Figure_synth_PAR.pdf", width=20, height=10)
par(mar=c(5,5,6,2))
plot(NULL, NULL, xlim=c(1,(nrow(plot_data)/2)), ylim=c(min(plot_data$value, na.rm=T), max(plot_data$value, na.rm=T)), 
     type="l", bty="l", ylab="", xaxt="n", cex.lab=2.2, cex.axis=2,
     xlab="Months", 
     main="Azerbaijan - H. Aliyev (1994-1996)", cex.main=3)
lines(lowess(plot_data[plot_data$var=="Treated",]$value,f=0.2), lwd=3, col="black")
lines(lowess(plot_data[plot_data$var=="Control",]$value,f=0.2), lwd=3, col="black", lty=2)
abline(v=10, col="maroon", lwd=4)
axis(1, at=c(0, 10, 22, 34), labels = c(10, 0, 12, 24), cex.axis=2)
#legend(0, max(plot_data$value),legend = c("Treated", "Control"), lty = c(1, 2), bty="n", lwd = 3, cex = 2.2)
#dev.off()

dev.off()

